package com.hlhj.carview.utils

import android.annotation.SuppressLint
import com.example.mymvp.log
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
import android.text.TextUtils





/**
 * Created by Never Fear   on 2019\1\14 0014.
Never More....
 */
object TimeUtils {
    fun secToTime(time: Int): String {
        var timeStr: String? = null
        var hour = 0
        var minute = 0
        var second = 0
        if (time <= 0)
            return "00:00"
        else {
            minute = time / 60
            if (minute < 60) {
                second = time % 60
                timeStr = unitFormat(minute) + ":" + unitFormat(second)
            } else {
                hour = minute / 60
                if (hour > 99)
                    return "99:59:59"
                minute = minute % 60
                second = time - hour * 3600 - minute * 60
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second)
            }
        }
        return timeStr
    }

    fun unitFormat(i: Int): String {
        var retStr: String? = null
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i)
        else
            retStr = "" + i
        return retStr
    }

    @SuppressLint("SimpleDateFormat")
//字符串转时间戳
    fun getTime(timeString: String?): String? {
        log("fhp","时间？${timeString}")
        var timeStamp: String? = null
        val sdf = SimpleDateFormat("yyyy-MM-dd")
        val d: Date
        try {
            d = sdf.parse(timeString)
            val l = d.getTime()
            timeStamp = l.toString()
        } catch (e: ParseException) {
            e.printStackTrace()
        }

        return timeStamp
    }

    fun timeStamp2Date(time: Long, format: String?): String {
        var t=0L
        if (time.toString().length<=10){
            t=time*1000
        }
        var format = format
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss"
        }
        val sdf = SimpleDateFormat(format)
        return sdf.format(Date(t))
    }


    fun getStringToDate(dateString: String, pattern: String): Long {
        val dateFormat = SimpleDateFormat(pattern)
        var date = Date()
        try {
            date = dateFormat.parse(dateString)
        } catch (e: ParseException) {
            // TODO Auto-generated catch block
            e.printStackTrace()
        }
        return date.time
    }

    fun timeLeft(t1:String?): Boolean {
        val time1 = t1?.toLong()!!/1000
        var timeLeft= time1!!-System.currentTimeMillis()/1000
        if (timeLeft>30*24*60*60){
            return false
        }else{
            return true
        }
    }

    //把String转化为float
    fun convertToFloat(number: String, defaultValue: Float): Float {
        if (TextUtils.isEmpty(number)) {
            return defaultValue
        }
        try {
            return java.lang.Float.parseFloat(number)
        } catch (e: Exception) {
            return defaultValue
        }

    }


}